/**
 * Yolema.com Inc.
 * Copyright (c) 2011-2012 All Rights Reserved.
 */
package org.grayrabbit.pay.chinapay.config;

import java.util.ResourceBundle;

import org.apache.commons.lang.StringUtils;
import org.grayrabbit.commons.util.PropertiesUtil;
import org.grayrabbit.pay.core.model.enums.PayResultEnum;
import org.grayrabbit.pay.core.model.exception.PayException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 银联配置信息
 * 
 * @author wy
 * @version $Id: ChinaPayConfig.java, v 0.1 2012-6-1 下午4:16:04 wy Exp $
 */
public class ChinaPayConfigUtil {
    
    private static final Logger  LOG = LoggerFactory.getLogger(ChinaPayConfigUtil.class);

    private static ChinaPayInfo chinaPayInfo = null;

    /**
     * 重置配置信息
     * 
     * @param chinaPayConfig
     */
    public static void reset(ChinaPayInfo chinaPayInfo) {
        ChinaPayConfigUtil.chinaPayInfo = chinaPayInfo;
    }

    /**
     * 初始化
     * 
     * @param chinaPayInfo
     */
    public static void init() {
        
        ResourceBundle resources = PropertiesUtil.getResourceBundle("config.chinapay.chinapay");
        if (resources == null) {
            LOG.error("找不到对应的银联配置信息..");
            throw new PayException(PayResultEnum.CHINAPAY_NOT_INIT);
        } else {
            ChinaPayInfo chinaPayInfo = new ChinaPayInfo();
            
            String memId = resources.getString("memId");
            if (StringUtils.isBlank(memId)) {
                throw new PayException("银联配置错误,memId为空");
            }
            String tradeUrl = resources.getString("tradeUrl");
            if (StringUtils.isBlank(tradeUrl)) {
                throw new PayException("银联配置错误,tradeUrl为空");
            }
            String refundUrl = resources.getString("refundUrl");
            if (StringUtils.isBlank(refundUrl)) {
                throw new PayException("银联配置错误,refundUrl为空");
            }
            
            chinaPayInfo.setMemId(memId);
            chinaPayInfo.setTradeUrl(tradeUrl);
            chinaPayInfo.setRefundUrl(refundUrl);
            reset(chinaPayInfo);
        }
    }
    
    /**
     * 获取配置的银联信息
     * 
     * @return
     */
    public static ChinaPayInfo getChinaPayInfo(){
        if(chinaPayInfo ==null){
            init();
        }
        return chinaPayInfo;
    }

}
